package com.ruoyi.business.controller;

import com.ruoyi.business.domain.model.*;
import com.ruoyi.business.domain.vo.EmployeeVo;
import com.ruoyi.business.service.IDegreesService;
import com.ruoyi.business.service.IDormitoryPlusService;
import com.ruoyi.business.service.IEmployeePlusService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 统计图
 *
 * @author songzhengsheng
 * @date 2023-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/index")
public class IndexController extends BaseController {
    private final IEmployeePlusService employeePlusService;
    private final IDormitoryPlusService dormitoryPlusService;
    private final IDegreesService degreesService;

    /**
     * 获取统计图数据
     */
    @GetMapping("/getData")
    public Map<String,Object> getData() {
        Map<String,Object> map=new HashMap<String,Object>();
        List<EmployeeClassification> list=employeePlusService.getEmployeeClassification();
        map.put("EmployeeClassification",list);
        Double ruzhuRate= dormitoryPlusService.getRate();
        map.put("ruzhuRate",ruzhuRate);

        AccommodationStats accommodationStatsEntity=employeePlusService.getAccommodationStats();
        map.put("initData3",accommodationStatsEntity);
        //用电量统计
        Double[] useDegreesArray=new Double[12];
        Arrays.fill(useDegreesArray, 0.0);
        List<useDegreesSumEntity> useDegreesSumList=degreesService.useDegreesSum(LocalDate.now().getYear());
        for (useDegreesSumEntity u : useDegreesSumList) {
            String useMonth = u.getUsemonth();
            Double totalSum = u.getTotalsum();
            // Extract the last two characters (month) from the useMonth string
            String month = useMonth.substring(useMonth.length() - 2);
            // Parse the month string to an integer (assuming the month is 01 to 12)
            int monthIndex = Integer.parseInt(month);
            // Subtract 1 from the month index to fit into the array (January is 01 but should be at index 0)
            monthIndex--;
            // Update the corresponding month index with the totalSum value
            useDegreesArray[monthIndex] = totalSum;
        }
        map.put("useDegreesArray",useDegreesArray);
        //未抄表数据
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年月
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        int day=currentDate.getDayOfMonth();
        // 转换成二维数组
        List<String[]> data = new ArrayList<>();
        if (day>=20){
            // 将日期固定为20号
            LocalDate fixedDate = LocalDate.of(year, month, 20);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 将LocalDate对象转换为指定格式的字符串
            String formattedDate = fixedDate.format(formatter);
            List<NoCheckDegreeDormitory> NoCheckDegreeDormitoryList=degreesService.getNoCheckDegreeDormitory(formattedDate);
            for (NoCheckDegreeDormitory dormitory : NoCheckDegreeDormitoryList) {
                String[] rowData = {
                    dormitory.getBuildingname(),
                    dormitory.getFloor(),
                    dormitory.getDormitorycode(),
                    dormitory.getCheckdate()
                };
                data.add(rowData);
            }
            map.put("NoCheckDegreeDormitoryList",data);
        }

        return map;
    }

    @GetMapping("/getNoCheckDegreeDormitoryList")
    public Map<String,Object> getNoCheckDegreeDormitoryList() {
        Map<String,Object> map=new HashMap<String,Object>();
        //未抄表数据
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年月
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        int day=currentDate.getDayOfMonth();
        if (day>=20){
            // 将日期固定为20号
            LocalDate fixedDate = LocalDate.of(year, month, 20);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 将LocalDate对象转换为指定格式的字符串
            String formattedDate = fixedDate.format(formatter);
            List<NoCheckDegreeDormitory> NoCheckDegreeDormitoryList=degreesService.getNoCheckDegreeDormitory(formattedDate);
            map.put("NoCheckDegreeDormitoryList",NoCheckDegreeDormitoryList);

        }
        return map;
    }
    /**
     * App端获取抄表历史信息
     * */
    @GetMapping("/getHisList/{b}")
    public  List<CheckDegreeHis> getHisList(@PathVariable String b) {
        String buildingname = b + "#楼";
        if(b.equals("9")){
            buildingname="车队";
        }
        return degreesService.getHisList(buildingname);

    }

    /**
     * 删除宿舍电度数
     *
     * @param id 主键串
     */
    @Log(title = "宿舍电度数", businessType = BusinessType.DELETE)
    @PostMapping("/removedegrees/{id}")
    public R<Void> removedegrees(@PathVariable Long id) {

        degreesService.del(id);
        return toAjax(true);
    }
}
